﻿using Maticsoft.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;

using System.Text;
using System.Windows.Forms;

namespace ZhiKeDuoServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DetectionPrintRequest();


        }

        private void DetectionPrintRequest()
        {
            string api = "http://hat.ohthink.com/sycapi/sync/";

            JsonSerializerSettings setting = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                Formatting = Formatting.None
            };
            try
            {
                ZhiKeDuoServer.BLL.ShopInfo bShop = new ZhiKeDuoServer.BLL.ShopInfo();
                ZhiKeDuoServer.Model.ShopInfo mShop = new ZhiKeDuoServer.Model.ShopInfo();
                mShop = bShop.GetModel(1);
                // 同步会员
                WebClient wc = new WebClient();
                ZhiKeDuoServer.BLL.Base<ZhiKeDuoServer.Model.MemberInfo> bmem = new ZhiKeDuoServer.BLL.Base<ZhiKeDuoServer.Model.MemberInfo>();
                List<ZhiKeDuoServer.Model.MemberInfo> lsmem = bmem.GetList("syc=0");
                foreach (ZhiKeDuoServer.Model.MemberInfo item in lsmem)
                {
                    string json = JsonConvert.SerializeObject(item);
                    wc.PostJson(api + "add/memberinfo/" + mShop.Shop, json);
                    item.syc = 1;
                    bmem.Update(item);
                }
                /// 同步订单
                /// 
                /// 
                ZhiKeDuoServer.BLL.Base<ZhiKeDuoServer.Model.SaleInfo> bSaleInfo = new ZhiKeDuoServer.BLL.Base<ZhiKeDuoServer.Model.SaleInfo>();
                List<ZhiKeDuoServer.Model.SaleInfo> lsSaleInfo = bSaleInfo.GetList("syc=0");



                foreach (ZhiKeDuoServer.Model.SaleInfo item in lsSaleInfo)
                {
                    string json = JsonConvert.SerializeObject(item, setting);
                    wc.PostJson(api + "add/saleinfo/" + mShop.Shop, json);
                    item.syc = 1;
                    bSaleInfo.Update(item);
                }
                /// 同步库存

                ZhiKeDuoServer.BLL.Base<ZhiKeDuoServer.Model.InGoods> bInGoods = new ZhiKeDuoServer.BLL.Base<ZhiKeDuoServer.Model.InGoods>();
                List<ZhiKeDuoServer.Model.InGoods> lsInGoods = bInGoods.GetList("syc=0");
                foreach (ZhiKeDuoServer.Model.InGoods item in lsInGoods)
                {
                    string json = JsonConvert.SerializeObject(item);
                    wc.PostJson(api + "add/ingoods/" + mShop.Shop, json);
                    item.syc = 1;
                    bInGoods.Update(item);
                }
                /// 同步分类
                ///
                ZhiKeDuoServer.BLL.Base<ZhiKeDuoServer.Model.Category> bCategory = new ZhiKeDuoServer.BLL.Base<ZhiKeDuoServer.Model.Category>();
                List<ZhiKeDuoServer.Model.Category> lsCategory = bCategory.GetList("syc=0");
                foreach (ZhiKeDuoServer.Model.Category item in lsCategory)
                {
                    string json = JsonConvert.SerializeObject(item);
                    wc.PostJson(api + "add/category/" + mShop.Shop, json);
                    item.syc = 1;
                    bCategory.Update(item);
                }
                //var ds_loadMenus = bsaleInfo.GetList("");//

                //foreach (var item in ds_loadMenus)
                //{
                //    _PrintTaskQueue.Enqueue(item.Id);
                //}


            }
            catch (Exception ex)
            {
                LogManager.WriteLog("Error", ex.Message);
            }


        }
    }
}
